﻿using Common;
using Common.NPOI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sample.Controllers
{
    [ApiController]
    [Route("[controller]")]
    [AllowAnonymous]
    public class ExportController : ControllerBase
    {
        [HttpGet()]
        public FileResult Excel()
        {
            List<ExcelModel> list = new List<ExcelModel>();
            Random rn = new Random();
            list.Add(new ExcelModel
            {
                Id = Snowflake.Next(),
                Code = Guid.NewGuid().ToString("N"),
                Name = "浩",
                CreateTime = DateTime.Now.AddDays(rn.Next(-30, 0)),
                Remarks = "备注呢"
            });
            list.Add(new ExcelModel
            {
                Id = Snowflake.Next(),
                Code = Guid.NewGuid().ToString("N"),
                Name = "腾",
                CreateTime = DateTime.Now.AddDays(rn.Next(-30, 0)),
                Remarks = "备注呢"
            });
            ExcelSheet<ExcelModel> sheet = new ExcelSheet<ExcelModel>(list);
            return sheet.Export(Guid.NewGuid().ToString("N") + ".xls");
        }


    }

    public class ExcelModel
    {
        [NPOIExcelColumn(Title = "序号", Order = 0)]
        public long Id { get; set; }
        [NPOIExcelColumn(Title = "名称", Order = 2)]
        public string Name { get; set; }
        [NPOIExcelColumn(Title = "编码", Order = 1)]
        public string Code { get; set; }
        [NPOIExcelColumn(Title = "创建时间", Order = 3)]
        public DateTime CreateTime { get; set; }
        [NPOIExcelColumn(Title = "备注信息", Order = 4)]
        public string Remarks { get; set; }
    }
}
